<?php
/**
 * gethosts
 * 
 * @link       http://code.google.com/p/gethosts/
 * @copyright  Copyright (c) 2010 shen2, <http://shen2.cn>
 * @license    MIT Style License
 * @version    0.1alpha
 */

/*	hosts.txt is changeable or not	*/
define('CHANGEABLE',true);

if (isset($_POST['hosts'])){
	$hosts_txt = $_POST['hosts'];
	if (CHANGEABLE) file_put_contents('hosts.txt',$hosts_txt);
}
else{
	$hosts_txt = file_get_contents('hosts.txt');
}
$hosts_array = preg_split('/[\s,]+/', $hosts_txt, -1, PREG_SPLIT_NO_EMPTY);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>gethosts</title>
	<script type="text/javascript">
	function copyToClipboard(copytext){
		if (window.clipboardData) window.clipboardData.setData('Text',copytext);
		else{
			var I="flashcopier";
			if(!document.getElementById(I)){var A=document.createElement("div");A.id=I;document.body.appendChild(A)}
			document.getElementById(I).innerHTML="";
			var D='<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURI(copytext)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
			document.getElementById(I).innerHTML=D;
		}
	}
	</script>
</head>
<body>
	<div style="float:left;margin:0 10px;">
		<h2>Blocked Hostnames</h2>
		<form method="post">
			<textarea name="hosts" style="width:300px;height:496px;margin:0;padding:3px;display:block;"><?php echo $hosts_txt;?></textarea>
			<button type="submit" style="margin: 5px auto; display: block;">Get Hosts</button>
		</form>
	</div>
	<div style="float:left;margin:0 10px;">
		<h2>hosts</h2>
		<pre id="hostsMap" style="width:400px;height:500px;margin:0;padding:3px;background-color:#F7F7F7;"><?php 
foreach($hosts_array as $hostname){
	if ($ip = gethostbyname($hostname))
		echo "$ip\t$hostname\n";
}?>
</pre>
		<button type="button" style="margin: 5px auto; display: block;" onclick="copyToClipboard(document.getElementById('hostsMap').innerHTML); alert('copyed');">Copy To Clipboard</button>
	</div>
</body>
</html>